﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using WPControla.Client.Services;
using WPControla.Client.ViewModels;
using WPControla.Client.Services.Rss;
using WPControla.Client.Services.Podcast;

namespace WPControla.Client.Test.ViewModels
{
    [TestClass]
    public class VMMainPageTest
    {
        Mock<INavigationService> navService = new Mock<INavigationService>(MockBehavior.Strict);
        Mock<IRssService> rssService = new Mock<IRssService>(MockBehavior.Strict);
        Mock<IPodcastService> podcastService = new Mock<IPodcastService>(MockBehavior.Strict);
        Mock<IAudioService> audioService = new Mock<IAudioService>(MockBehavior.Strict);

        /// <summary>
        /// Initialize viewmodel with mock objects.
        /// </summary>
        /// <returns></returns>
        private IVMMainPage InitializeViewModel()
        {
            //Generate mocks.
            rssService.Setup(rs => rs.GetRssData(It.IsAny<Uri>())).Verifiable();
            podcastService.Setup(ps => ps.GetPodcastData(It.IsAny<Uri>())).Verifiable();

            return new VMMainPage(navService.Object, rssService.Object, podcastService.Object, audioService.Object);
        }

        [TestMethod]
        public void GetSetChaptersList()
        {
            IVMMainPage target = InitializeViewModel();

            var Chapters = new Entities.Podcast() 
            {
                Items = new List<Entities.PodcastItem>() 
                    { new Entities.PodcastItem() 
                        { Title = "Test podcast" } } 
            };

            target.ChaptersList = Chapters;

            Assert.AreEqual(Chapters, target.ChaptersList);
        }

        [TestMethod]
        public void GetSetSelectedChapterTest()
        {
            IVMMainPage target = InitializeViewModel();

            Entities.PodcastItem item = new Entities.PodcastItem() { id = 1, Title = "test"};

            navService.Setup(ns => ns.NavigateToChapter(It.Is<Entities.PodcastItem>(t => t.Title == item.Title))).Verifiable();

            target.SelectedChapter = item;

            navService.Verify(ns => ns.NavigateToChapter(It.Is<Entities.PodcastItem>(t => t.Title == item.Title)), Times.Once());

            Assert.AreEqual(item, target.SelectedChapter);
        }

        [TestMethod]
        public void GetSetArticlesList()
        {
            IVMMainPage target = InitializeViewModel();

            var Articles = new Entities.Rss()
            {
                Items = new List<Entities.RssItem>() 
                    { new Entities.RssItem() 
                        { Title = "Test Item" } }
            };

            target.ArticlesList = Articles;

            Assert.AreEqual(Articles, target.ArticlesList);
        }

        [TestMethod]
        public void GetSetTwitterList()
        {
            IVMMainPage target = InitializeViewModel();

            var Twitter = new Entities.Rss()
            {
                Items = new List<Entities.RssItem>() 
                    { new Entities.RssItem() 
                        { Title = "Test Item" } }
            };

            target.TwitterList = Twitter;

            Assert.AreEqual(Twitter, target.TwitterList);
        }

        [TestMethod]
        public void GetPreviewAudioCommand()
        {
            IVMMainPage target = InitializeViewModel();

            var actual = target.PreviewAudioCommand;

            Assert.IsNotNull(actual);
        }

        [TestMethod]
        public void ExecutePreviewAudioCommandTest()
        {
            IVMMainPage target = InitializeViewModel();

            audioService.Setup(auds => auds.PreviewTrack(It.IsAny<Entities.PodcastItem>())).Verifiable();

            target.ExecutePreviewAudioCommand(new Entities.PodcastItem());

            audioService.Verify(auds => auds.PreviewTrack(It.IsAny<Entities.PodcastItem>()), Times.Once());
        }

        [TestMethod]
        public void PodcastCompletedTest()
        {
            IVMMainPage target = InitializeViewModel();

            PodcastDownloadEventArgs args = new PodcastDownloadEventArgs(new Entities.Podcast() { Items = new List<Entities.PodcastItem>() { new Entities.PodcastItem()} });

            target.Podcast_Completed(this, args);

            Assert.AreEqual(1, target.ChaptersList.Items.Count);
        }

        [TestMethod]
        public void ArticlesCompletedTest()
        {
            IVMMainPage target = InitializeViewModel();

            RssDownloadEventArgs args = new RssDownloadEventArgs(new Entities.Rss() { Items = new List<Entities.RssItem>() { new Entities.RssItem() } });

            target.RssArticles_Completed(this, args);

            Assert.AreEqual(1, target.ArticlesList.Items.Count);
        }

        [TestMethod]
        public void TwitterCompletedTest()
        {
            IVMMainPage target = InitializeViewModel();

            RssDownloadEventArgs args = new RssDownloadEventArgs(new Entities.Rss() { Items = new List<Entities.RssItem>() { new Entities.RssItem() } });

            target.RssTwitter_Completed(this, args);

            Assert.AreEqual(1, target.TwitterList.Items.Count);
        }
    }
}
